<template>
  <div>
    <!-- 弹出层的头部区域 -->
    <van-nav-bar title="频道管理">
      <template #right>
        <van-icon name="cross" size="0.37333334rem" color="white" @click="closeChannelFn"/>
      </template>
    </van-nav-bar>
    <!-- 我的频道 -->
    <div class="my-channel-box">
      <div class="channel-title">
        <span>我的频道
          <span class="small-title">
            点击{{ isShow ? '删除' : '进入'}} 频道
          </span>
        </span>
        <span @click="editfn">{{isShow ? '完成' : '编辑' }}</span>
      </div>
      <!-- 我的频道列表 -->
      <van-row type="flex">
        <van-col span="6" v-for="item in userChannelList" :key="item.id" @click="delChannelFn(item)">
          <div class="channel-item van-hairline--surround">
            {{item.name}}
            <!-- 删除的徽标 -->
            <van-badge color="transparent" class="cross-badge" v-if="isShow && item.id !==0">
              <template #content>
                <van-icon
                  name="cross"
                  class="badge-icon"
                  color="#cfcfcf"
                  size="0.32rem"
                />
              </template>
            </van-badge>
          </div>
        </van-col>
      </van-row>
    </div>

    <!-- 更多频道 -->
    <div class="more-channel-box">
      <div class="channel-title">
        <span>点击添加更多频道：</span>
      </div>
      <!-- 更多频道列表 -->
      <van-row type="flex">
        <!-- 点击的是谁 要传参（item） -->
        <van-col span="6" v-for="item in unChannelList" :key="item.id" @click="addMore(item)">
          <div class="channel-item van-hairline--surround">{{item.name}}</div>
        </van-col>
      </van-row>
    </div>
  </div>
</template>

<script>
import { getListAPI, upLoadUserChannelAPI } from '@/api'
export default {
  name: 'ChannelEdit',
  props: {
    userChannelList: Array // 用户已选频道列表
  },

  data () {
    return {
      allChannelList: [], // 存放接口请求到的所有频道列表
      isShow: false // 控制徽标显示与否
    }
  },
  created () {
    this.getAllChannelList()
  },
  methods: {
    // 获取所有频道列表
    async getAllChannelList () {
      const res = await getListAPI()
      // console.log(res)
      this.allChannelList = res.data.data.channels
      // console.log(this.userChannelList)
      // console.log(this.allChannelList)
    },
    // 点击编辑
    editfn () {
      if (this.isShow) {
        // this.$nextTick(() => {
        this.upUserloadChannelFn()
        // })
      }
      this.isShow = !this.isShow
    },
    // 更多频道点击事件==>新增
    addMore (obj) { // obj==>{频道id和name}
      // console.log(obj)
      // 编辑 状态时才能触发
      if (this.isShow) {
        // 点击就添加到我的频道  props接收的数据不能直接添加所以要用父级要用.sync
        // 相当于是 @update:userChannelList=val => val = userChannelList
        const arr = [...this.userChannelList]
        arr.push(obj)
        this.$emit('update:userChannelList', arr)
        // .sync是异步的所有要用this.$nextTick调用函数
        // this.$nextTick(() => {
        //   this.upUserloadChannelFn()
        // })
      }
    },
    // 上传(设置)频道列表 提交到服务器
    async upUserloadChannelFn () {
      const newArr = this.userChannelList.map((obj, index) => {
        return {
          id: obj.id,
          seq: index
        }
      })
      await upLoadUserChannelAPI({
        channels: newArr
      })
    },
    // 删除我的频道列表
    delChannelFn (channelObj) {
      // 如果是true则删除 否则 切换频道(子传父)
      if (this.isShow === true && channelObj.id !== 0) {
        const arr = [...this.userChannelList]
        // findIndex方法 找符合条件的下标 找到返回下标
        const index = arr.findIndex(obj => obj.id === channelObj.id)
        arr.splice(index, 1)
        this.$emit('update:userChannelList', arr) // 通知父组件数据重新赋值
      } else {
        // 切换频道(子传父)自定义事件
        this.$emit('changeChannelEV', channelObj)
        // 切换之后关闭弹框
        this.$emit('input', false)
      }
    },
    // 点击X关闭弹框编辑按钮恢复成完成
    closeChannelFn () {
      this.show = false
      this.$emit('input', false) // 关闭弹框
    }
  },
  // 计算属性 数据发生改变会重新计算 所以点击频道时 没有删除代码自动会减少
  computed: {
    unChannelList () {
      const newArr = []

      this.allChannelList.forEach(item => {
        const targetObj = this.userChannelList.find(obj => {
          return item.id === obj.id
        })
        if (targetObj === undefined) {
          newArr.push(item)
        }
        // console.log(newArr)
      })
      return newArr
    }

  }

}
</script>
<style scoped lang="less">
.van-popup,
.popup-container {
  background-color: transparent;
  height: 100%;
  width: 100%;
}

.popup-container {
  display: flex;
  flex-direction: column;
}

.pop-header {
  height: 90px;
  background-color: #007bff;
  color: white;
  text-align: center;
  font-size: 14px;
  position: relative;
  .title {
    width: 100%;
    position: absolute;
    bottom: 15px;
  }
}

.pop-body {
  flex: 1;
  overflow: scroll;
  padding: 8px;
  background-color: white;
}

.my-channel-box,
.more-channel-box {
  .channel-title {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    line-height: 28px;
    padding: 0 6px;
  }
}

.channel-item {
  font-size: 12px;
  text-align: center;
  line-height: 36px;
  background-color: #fafafa;
  margin: 5px;
}

/*删除的微标 */
.cross-badge {
  position: absolute;
  right: -3px;
  top: 0;
  border: none;
}

/*提示文字 */
.small-title {
  font-size: 10px;
  color: gray;
}
/* 频道编辑弹出层铺满屏幕 */

</style>
